﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Drawing.Text;
using System.IO;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Runtime.InteropServices;

namespace Project_Origin
{
    public partial class PO_Classical_Mode_UI : Form
    {
        //最小值
        public int Min { get; set; }

        //最大值
        public int Max { get; set; }

        //随机数
        public int I { get; set; }

        //猜测次数
        public int Time { get; set; }

        //记录难易程度
        public int Easy { get; set; }

        PrivateFontCollection pfc = new PrivateFontCollection();
        
        //初始化
        public PO_Classical_Mode_UI()
        {
            InitializeComponent();
            //Setfont();
        }
        //初始化
        public PO_Classical_Mode_UI(int easy)
        {
            InitializeComponent();
            this.Min = 1;
            this.Easy = easy;
            switch (easy)
            {
                case 1: this.Max = 100;break;
                case 2: this.Max = 1000;break;
                case 3: this.Max = 10000;break;   
            }
            //赋值
            PO_MIN.Text = this.Min.ToString();
            PO_MAX.Text = this.Max.ToString();
            //生成随机数并赋值
            Random random = new Random();
            this.I = random.Next(this.Min, this.Max);
        }
        
        private Point mousePoint = new Point();

        //窗口移动组件
        private void PO_Classical_Mode_UI_MouseDown(object sender, MouseEventArgs e)
        {
            this.mousePoint.X = e.X;
            this.mousePoint.Y = e.Y;
        }

        private void PO_Classical_Mode_UI_MouseMove(object sender, MouseEventArgs e)
        {
            if (e.Button == MouseButtons.Left)
            {
                this.Top = Control.MousePosition.Y - mousePoint.Y + 13;
                this.Left = Control.MousePosition.X - mousePoint.X + 13;
            }

        }
        //退出按键
        private void PO_Button_Exit_Click(object sender, EventArgs e)
        {
            PO_Timer0.Start();
        }

        //最小化
        private void PO_MixNum_Button_Click(object sender, EventArgs e)
        {
            this.WindowState = FormWindowState.Minimized;
        }

        //timer，用以关闭窗口
        private void PO_Timer0_Tick(object sender, EventArgs e)
        {
            if (this.Opacity >= 0.3)
                this.Opacity -= 0.3;
            else
            {
                PO_Timer0.Stop();
                this.Close();
                Environment.Exit(Environment.ExitCode);
            }
        }

        //关闭函数（父窗体内的定义）
        public void closethis()
        {
            this.Dispose();
            this.Close();
        }

        
        //返回组件（需要和子窗体的代码块配合使用）
        private void PO_Button_Back_Click(object sender, EventArgs e)
        { 
            PO_Confirm_Form pO_Confirm_Form = new PO_Confirm_Form();
            if (pO_Confirm_Form != null)
            {
                if (pO_Confirm_Form.IsDisposed)
                    pO_Confirm_Form = new PO_Confirm_Form();//如果已经销毁，则重新创建子窗口对象
                pO_Confirm_Form.closefather += new PO_Confirm_Form.childclose(this.closethis);
                pO_Confirm_Form.ShowDialog(this);
                pO_Confirm_Form.Focus();
            }
            else
            {
                pO_Confirm_Form = new PO_Confirm_Form();
                pO_Confirm_Form.closefather += new PO_Confirm_Form.childclose(this.closethis);
                pO_Confirm_Form.ShowDialog(this);
                pO_Confirm_Form.Focus();
            }
        }

        //确定键
        private void PO_Button_Deciside_Click(object sender, EventArgs e)
        {
            //赋值并清除编辑框内容
            if (pO_B_TextBox1.Text != "")
            {
                int num = int.Parse(pO_B_TextBox1.Text);
                GameIs(Game(num));
                pO_B_TextBox1.Clear();
            }
            
        }

        //编辑框事件
        private void pO_B_TextBox1_KeyPress(object sender, KeyPressEventArgs e)
        {
            //如果是回车
            if (e.KeyChar == Convert.ToChar(System.Windows.Forms.Keys.Enter))
            {
                if (pO_B_TextBox1.Text != "")
                {
                    int num = int.Parse(pO_B_TextBox1.Text);
                    GameIs(Game(num));
                    pO_B_TextBox1.Clear();
                }
            }
            if (e.KeyChar == 0x20) e.KeyChar = (char)0;  //禁止空格键
            if ((e.KeyChar == 0x2D) && (((TextBox)sender).Text.Length == 0)) return;   //处理负数
            if (e.KeyChar > 0x20)
            {
                try
                {
                    double.Parse(((TextBox)sender).Text + e.KeyChar.ToString());
                }
                catch
                {
                    e.KeyChar = (char)0;   //处理非法字符
                }
            }
        }

        




        //**************************************************
        //华丽的分割线

        //游戏重置 传入游戏难易程度
        public void GameRestrat(int easy)
        {
            //游戏初始化
            this.Min = 1;
            switch (easy)
            {
                case 1: this.Max = 100; break;
                case 2: this.Max = 1000; break;
                case 3: this.Max = 10000; break;
            }
            //游戏次数重置
            this.Time = 0;
            //显示初始化
            PO_MIN.Text = this.Min.ToString();
            PO_MAX.Text = this.Max.ToString();

            //字体初始化
            string AppPath = Application.StartupPath;
            PrivateFontCollection privateFonts = new PrivateFontCollection();
            privateFonts.AddFontFile(AppPath + @"\Resources\站酷快乐体.ttf");//加载字体
            Font font = new Font(privateFonts.Families[0], 20);
            PO_MAX.Font = font;
            PO_MIN.Font = font;
            pO_B_TextBox1.Font = font;
            PO_Game_Lable.Font = font;

        }

        //游戏状态
        public enum GameStatus
        {
            //数字过大
            LargeNumber,
            //超出范围
            OutOfRange,
            //数字过小
            SmallNumber,
            //正确的数字
            TrueNumber,
        }

        //游戏结果判断
        public void GameIs(GameStatus gamestatus)
        {
            switch (gamestatus)
            {
                case GameStatus.LargeNumber: PO_Game_Lable.Text = "数字太大了哦！"; break;
                case GameStatus.OutOfRange: PO_Game_Lable.Text = "不会看范围吗？"; break;
                case GameStatus.SmallNumber: PO_Game_Lable.Text = "数字太小了哦！"; break;
                case GameStatus.TrueNumber:
                    PO_If_Next_Stage next = new PO_If_Next_Stage(this.Time,3);
                    next.ShowDialog();
                    if (next.Status == 1)
                        this.Close();
                    else
                        GameRestrat(this.Easy);
                    break;

            }
        }

        //游戏主体 返回游戏状态
        public GameStatus Game(int number)
        {
            //符合范围
            if (number >= this.Min && number <= this.Max)
            {
                if (number > this.I)
                {
                    //最大值赋值
                    this.Max = number;
                    //次数加一
                    this.Time++;
                    //赋值变化
                    PO_MAX.Text = this.Max.ToString();
                    //大于随机数 返回太大
                    return GameStatus.LargeNumber;
                }
                else if (number < this.I)
                {
                    //最小值赋值
                    this.Min = number;
                    //次数加一
                    this.Time++;
                    //赋值变化
                    PO_MIN.Text = this.Min.ToString();
                    //小于随机数 返回太小                   
                    return GameStatus.SmallNumber;
                }
                //满足条件
                else
                {
                    //次数加一
                    this.Time++;
                    return GameStatus.TrueNumber;
                }

            }
            else  //不符合范围
            {
                //返回超出范围
                if (number > this.Max)
                    return GameStatus.OutOfRange;
                else
                    return GameStatus.OutOfRange;
            }
        }

        //字体设置

        
        private void PO_Classical_Mode_UI_Load(object sender, EventArgs e)
        {
            string AppPath = Application.StartupPath;
            PrivateFontCollection privateFonts = new PrivateFontCollection();
            try
            {
                privateFonts.AddFontFile(AppPath + @"\Resources\站酷快乐体.ttf");//加载字体
                Font font = new Font(privateFonts.Families[0], 20);
                PO_MAX.Font = font;
                PO_MIN.Font = font;
                pO_B_TextBox1.Font = font;
                PO_Game_Lable.Font = font;
            }
            catch
            {
                MessageBox.Show("字体不存在或加载失败\n程序将以默认字体显示", "警告", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
            }
        }

        


        private void pO_B_TextBox1_Enter(object sender, EventArgs e)
        {
            ImeMode = ImeMode.Disable;
        }
    }
}
